package com.kuyun.base.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class HttpDownLoad {
	private String workPath = null;
	private String downLoadUrl = null;
	private DefaultHttpClient httpClient = new DefaultHttpClient();

	public void downLoad(String url, String dst) {
		OutputStream out = null;
		InputStream in = null;
		try {
			HttpGet httpGet = new HttpGet(url);
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity entity = httpResponse.getEntity();
			in = entity.getContent();
			long length = entity.getContentLength();
			if (length <= 0) {
				System.out.println("下载文件不存在！");
				return;
			}
			out = new FileOutputStream(new File(dst));
			saveTo(in, out);
		} catch (Exception e) {
		}
	}
	public void saveTo(InputStream in, OutputStream out) throws Exception {
		byte[] data = new byte[1024 * 1024];
		int index = 0;
		while ((index = in.read(data)) != -1) {
			out.write(data, 0, index);
		}
		in.close();
		out.close();
	}
}
